<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\common\library\Ems;
use app\common\library\Sms;
use fast\Random;
use think\Validate;

class Usercenter extends Api
{

    protected $noNeedLogin = ['*'];
    protected $noNeedRight = '*';

    public function _initialize()
    {
        parent::_initialize();
        header('Access-Control-Allow-Origin:*');
    }

    /**
     * 会员中心
     */
    public function index()
    {
        $this->view->assign('title', __('User center'));
        return $this->view->fetch();
    }

    /**
     * 个人订单展示
     */
    public function order()
    {
        $user_id = input('user_id');
        $status = input('status');
        $order = db('order')->where('user_id',$user_id)->where('status',$status)->order('updatetime desc')->select();
        // dump($users);exit;
        if($order){
            return json(['code'=>1,'res'=>1,'msg'=>'操作成功','data'=>$order]);
        } else {
            return json(['code'=>1,'res'=>0,'msg'=>'暂无订单']);
        }
    }

    public function address()
    {
        $user_id = input('user_id');
        $name = input('name');
        $phone = input('phone');
        $add['address'] = input('address');
        $addr = db('address')->where('user_id',$user_id)->find();
        if($addr){
            $add['name'] = $name;
            $add['phone'] = $phone;
            $add['updatetime'] = time();
            $res = db('address')->where('user_id',$user_id)->update($add);
            if($res){
                return json(['code'=>1,'res'=>1,'msg'=>'修改成功','data'=>$add]);
            } else {
                return json(['code'=>1,'res'=>0,'msg'=>'修改失败','data'=>$add]);
            }
        } else {
            $add['user_id'] = $user_id;
            $add['name'] = $name;
            $add['phone'] = $phone;
            $add['addtime'] = time();
            $add['updatetime'] = time();
            $res = db('address')->insert($add);
            if($res){
                return json(['code'=>1,'res'=>1,'msg'=>'添加成功','data'=>$add]);
            } else {
                return json(['code'=>1,'res'=>0,'msg'=>'添加失败','data'=>$add]);
            }
        }
    }

}
